package zju.dgm.dto;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author lzy
 */
@Getter
@Setter
@ToString
public class RegionEntry implements Serializable,Comparable<RegionEntry> {
    ServerInfo server;
    int qTimes;

    /**
     * 用于JSON序列化
     */
    public RegionEntry(){}

    public RegionEntry(ServerInfo server, int qTimes) {
        this.server = server;
        this.qTimes = qTimes;
    }

    public void increment(int delta){
        qTimes+=delta;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        RegionEntry that = (RegionEntry) o;
        return server.equals(that.server);
    }

    @Override
    public int hashCode() {
        return Objects.hash(server);
    }

    @Override
    public int compareTo(RegionEntry o) {
        if (getQTimes() != o.getQTimes()) {
            return getQTimes() - o.getQTimes();
        } else if (getServer().getPort() != o.getServer().getPort()) {
            return getServer().getPort() - o.getServer().getPort();
        } else {
            return 1;
        }
    }
}